package org.easyx.ui.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.easyx.core.entity.Constants;
import org.easyx.core.utils.DateKit;
import org.easyx.core.utils.ObjKit;
import org.easyx.ui.base.BossEasyUIBaseController;
import org.easyx.ui.shiro.captcha.DreamCaptcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class CommonsController extends BossEasyUIBaseController{
	private static final Logger log = LoggerFactory.getLogger(CommonsController.class);
	@Autowired
    private DreamCaptcha dreamCaptcha;
	
	 /**
     * 图形验证码
     */
    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        dreamCaptcha.generate(request, response);
    }
    
    /**
     * webuploader文件上传功能
     */
    @RequestMapping("pictures/save")
    @ResponseBody
    public Object webUploaderSave(@RequestParam(required=false) MultipartFile file) {
    	String basePath = getContextPath();
    	String fileDateName = DateKit.getDays();
    	String imgPath = Constants.IMG_SAVE_PATH +fileDateName +File.separator ;//文件上传相对路径，相对与项目的webroot
    	String filePath = basePath + imgPath;//文件上传绝对路径
    	String fileName = "";
    	if (null != file && !file.isEmpty()) {
			fileName = fileUp(file, filePath, ObjKit.getUUId());//执行上传
		}else{
			log.info("上传失败：file is null");
		}
    	
    	log.debug("文件上传成功：绝对路径="+filePath +File.separator+fileName);
    	log.debug("文件上传成功：相对路径="+imgPath +File.separator+fileName);
    	
    	return operateSuccess("文件上传成功",imgPath +File.separator+fileName) ;
    }
    
    /**
	 * @param file 			//文件对象
	 * @param filePath		//上传路径
	 * @param fileName		//文件名
	 * @return  文件名
	 */
	private String fileUp(MultipartFile file, String filePath, String fileName){
		String extName = ""; // 扩展名格式：
		try {
			if (file.getOriginalFilename().lastIndexOf(".") >= 0){
				extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			}
			copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "");
		} catch (IOException e) {
			System.out.println(e);
		}
		return fileName+extName;
	}
	
	/**
	 * 写文件到当前目录的upload目录中
	 * 
	 * @param in
	 * @param fileName
	 * @throws IOException
	 */
	private String copyFile(InputStream in, String dir, String realName)
			throws IOException {
		File file = new File(dir, realName);
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		FileUtils.copyInputStreamToFile(in, file);
		return realName;
	}
    
    /**
     * 查看 图标
     */
    @RequestMapping("foundIcons")
    public String foundIcons() {
    	log.debug("进入查看系统图标页面...");
    	return "foutIcons";
    }
}
